package cn.iocoder.yudao.module.trade.controller.app.giftcard;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitRespVO;
import cn.iocoder.yudao.module.trade.controller.app.giftcard.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftCardGroupDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftWxCardDO;
import cn.iocoder.yudao.module.trade.service.giftcard.GiftCardGroupService;
import cn.iocoder.yudao.module.trade.service.giftcard.GiftCardService;
import cn.iocoder.yudao.module.trade.service.giftcard.GiftWxCardService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 礼品卡接口")
@RestController
@RequestMapping("/trade/giftcard")
@Validated
@Slf4j
public class AppGiftCardController {

    @Resource
    private GiftWxCardService giftWxCardService;
    @Resource
    private GiftCardService giftCardService;
    @Resource
    private GiftCardGroupService giftCardGroupService;

    @GetMapping("/giftWxCardList")
    @Operation(summary = "礼品卡样式列表")
    public CommonResult<List<GiftWxCardDO>> giftWxCardList() {
        return success(giftWxCardService.getGiftWxCardDOList());
    }

    @GetMapping("/selectGiftCardGroupList")
    @Operation(summary = "根据wxCardId获取组合")
    public CommonResult<List<GiftCardGroupDO>> selectGiftCardGroupList(@RequestParam("cardId") String cardId,
                                                                       @RequestParam(value = "groupType", required = false) Integer groupType) {
        if (null == groupType) {
            groupType = 0;
        }
        return success(giftCardGroupService.selectGiftCardGroupList(cardId, groupType));
    }

    @PostMapping("/createGiftCardOrder")
    @Operation(summary = "创建订单与礼品卡 返回支付订单信息")
    @PreAuthenticated
    public CommonResult<PayOrderSubmitRespVO> createGiftCardOrder(@RequestBody AppCreateGiftCardReqVO appCreateGiftCardReqVO) {
        return success(giftCardService.createGiftCardOrder(appCreateGiftCardReqVO));
    }

    @PostMapping("/notGiftCardOrder")
    @Operation(summary = "未支付订单信息")
    public CommonResult<PayOrderSubmitRespVO> notGiftCardOrder(@RequestBody AppGiftCardNotPayVO appGiftCardNotPayVO) {
        return success(giftCardService.notGiftCardOrder(appGiftCardNotPayVO));
    }

    @GetMapping("/detailGiftCardByCardId")
    @Operation(summary = "使用礼品卡详情页数据")
    public CommonResult<JSONObject> detailGiftCardByCardId(@RequestParam("cardId") Long cardId) {
        return success(giftCardService.detailGiftCardByCardId(cardId));
    }

    @PostMapping("/queryGiftCardByStatus")
    @Operation(summary = "根据状态获取礼品卡列表")
    public CommonResult<List<AppGiftCardUseListVO>> queryGiftCardByStatus(@RequestBody AppGiftCardListVO appGiftCardListVO) {
        return success(giftCardService.queryGiftCardByStatus(appGiftCardListVO.getStatus(), appGiftCardListVO.getOpenId()));
    }

    @GetMapping("/receive")
    @Operation(summary = "赠送礼品卡")
    public CommonResult<Integer> receiveGiftCard(@RequestParam("cardId") Long cardId,
                                                 @RequestParam(value = "nickName", required = false) String nickName,
                                                 @RequestParam(value = "avatar", required = false) String avatar) {
        return success(giftCardService.receiveGiftCard(cardId, nickName, avatar));
    }

    @GetMapping("/lisy")
    @Operation(summary = "立即使用")
    public CommonResult<Integer> lisy(@RequestParam("cardId") Long cardId, @RequestParam("openId") String openId) {
        return success(giftCardService.lisy(cardId, openId));
    }


    @GetMapping("/revokeGive")
    @Operation(summary = "撤销赠送礼品卡")
    public CommonResult<Integer> revokeGive(@RequestParam("cardId") Long cardId) {
        return success(giftCardService.revokeGive(cardId));
    }

    @GetMapping("/give")
    @Operation(summary = "领取礼品卡")
    public CommonResult<Integer> giveGiftCard(@RequestParam("cardId") Long cardId,
                                              @RequestParam("openId") String openId,
                                              @RequestParam(value = "nickName", required = false) String nickName,
                                              @RequestParam(value = "avatar", required = false) String avatar) {
        return success(giftCardService.giveGiftCard(cardId, openId, nickName, avatar));
    }

    @GetMapping("/giftCardReceiveGive")
    @Operation(summary = "获取领取赠送信息")
    public CommonResult<AppSomeVO> giftCardReceiveGive(@RequestParam("cardId") Long cardId) {
        return success(giftCardService.giftCardReceiveGive(cardId));
    }

    @GetMapping("/useGiveCard")
    @Operation(summary = "使用礼品卡 立即兑换 立即使用")
    public CommonResult<Integer> useGiveCard(@RequestParam("cardId") Long cardId, @RequestParam("addressId") Long addressId, @RequestParam("openId") String openId) {
        return success(giftCardService.useGiveCard(cardId, addressId, openId));
    }

}
